package com.tc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.dao.ComdTrucksMapper;
import com.tc.pojo.ComdTrucks;
import com.tc.service.ComdTrucksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:ComdTrucks业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class ComdTrucksServiceImpl implements ComdTrucksService {

    @Autowired
    private ComdTrucksMapper comdTrucksMapper;


    /**
     * ComdTrucks条件+分页查询
     * @param comdTrucks 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<ComdTrucks> findPage(ComdTrucks comdTrucks, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(comdTrucks);
        //执行搜索
        return new PageInfo<ComdTrucks>(comdTrucksMapper.selectByExample(example));
    }

    /**
     * ComdTrucks分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<ComdTrucks> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<ComdTrucks>(comdTrucksMapper.selectAll());
    }

    /**
     * ComdTrucks条件查询
     * @param comdTrucks
     * @return
     */
    @Override
    public List<ComdTrucks> findList(ComdTrucks comdTrucks){
        //构建查询条件
        Example example = createExample(comdTrucks);
        //根据构建的条件查询数据
        return comdTrucksMapper.selectByExample(example);
    }


    /**
     * ComdTrucks构建查询对象
     * @param comdTrucks
     * @return
     */
    public Example createExample(ComdTrucks comdTrucks){
        Example example=new Example(ComdTrucks.class);
        Example.Criteria criteria = example.createCriteria();
        if(comdTrucks!=null){
            // 编号
            if(!StringUtils.isEmpty(comdTrucks.getId())){
                    criteria.andEqualTo("id",comdTrucks.getId());
            }
            // 关联的商家ID
            if(!StringUtils.isEmpty(comdTrucks.getCid())){
                    criteria.andEqualTo("cid",comdTrucks.getCid());
            }
            // 关联的货车ID
            if(!StringUtils.isEmpty(comdTrucks.getTid())){
                    criteria.andEqualTo("tid",comdTrucks.getTid());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        comdTrucksMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改ComdTrucks
     * @param comdTrucks
     */
    @Override
    public void update(ComdTrucks comdTrucks){
        comdTrucksMapper.updateByPrimaryKey(comdTrucks);
    }

    /**
     * 增加ComdTrucks
     * @param comdTrucks
     */
    @Override
    public void add(ComdTrucks comdTrucks){
        comdTrucksMapper.insert(comdTrucks);
    }

    /**
     * 根据ID查询ComdTrucks
     * @param id
     * @return
     */
    @Override
    public ComdTrucks findById(Integer id){
        return  comdTrucksMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询ComdTrucks全部数据
     * @return
     */
    @Override
    public List<ComdTrucks> findAll() {
        return comdTrucksMapper.selectAll();
    }
}
